package com.binguo.wxapi.service.redis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.binguo.socket.SocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;

import java.util.Base64;

@Component
public class RedisSubscriber extends MessageListenerAdapter {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public void onMessage(Message message, byte[] bytes) {
        String sendMsg = message.toString();
        sendMsg = sendMsg.substring(1, sendMsg.length() - 1);
        System.out.println(new String(Base64.getDecoder().decode(sendMsg)));
        JSONObject sendParam = JSON.parseObject(new String(Base64.getDecoder().decode(sendMsg)));
        String deskId = sendParam.getInteger("deskId").toString();
        sendParam.remove("deskId");
        Integer logId = sendParam.getInteger("id");
        if (SocketService.socketSet.get(deskId) != null) {
            SocketService.sendtoUser(sendParam.toJSONString(), deskId, logId);
        }
    }
}